package org.emet.service;

import java.util.List;

import org.emet.dao.DataDicDao;
import org.emet.dao.DataItemDao;
import org.emet.jee.BusinessException;
import org.emet.jee.console.entity.sys.DataDic;
import org.emet.jee.console.entity.sys.DataItem;
import org.emet.jee.console.services.SequenceGenarator;
import org.emet.jee.core.dao.Page;
import org.emet.service.dto.DataDicCriteria;
import org.hibernate.ObjectNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RemotingDestination
public class DataDicService {
	@Autowired
	private DataDicDao dataDicDao;
	@Autowired
	private DataItemDao dataItemDao;
	@Autowired
	private SequenceGenarator genarator;

	private static Logger logger = LoggerFactory.getLogger(DataDicService.class);

	public void save(DataDic entity) {
		try {
			dataDicDao.save(entity);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	@Transactional(readOnly = true)
	public List<DataDic> getAll() {
		try {
			List<DataDic> all = dataDicDao.getAll();
			return all;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	@Transactional(readOnly = true)
	public List<DataDic> getAllValidItems() {
		try {
			dataDicDao.getSession().enableFilter("allValidItemsFilter").setParameter("_isEnabled", true);
			List<DataDic> all = dataDicDao.getAll();
			return all;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	public void saveDataItem(DataItem entity) {
		try {
			if (entity.getId() == null) {
				if (entity.getKey() == null || entity.getKey().equals("")) {
					String seq = genarator.generate(1L);
					entity.setKey(seq);
				}
			}
			dataItemDao.save(entity);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	//	@Transactional(readOnly = true)
	//	public Set<DataItem> getCachedItems(Long dicId) {
	//		try {
	//			return dataDicDao.get(dicId).getItems();
	//		} catch (Exception e) {
	//			logger.error(e.getMessage(), e);
	//			throw new BusinessException("发生异常!");
	//		}
	//	}

	@Transactional(readOnly = true)
	public List<DataItem> getEnabledItems(Long id) {
		try {
			return dataItemDao.find("from DataItem where parent.id=? and enabled = true", id);
		} catch (ObjectNotFoundException e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	@Transactional(readOnly = true)
	public List<DataItem> getItems(Long id) {
		try {
			return dataItemDao.findBy("parent.id", id);
		} catch (ObjectNotFoundException e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	@Transactional(readOnly = true)
	public DataDic get(Long id) {
		try {
			return dataDicDao.get(id);
		} catch (ObjectNotFoundException e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	@Transactional(readOnly = true)
	public DataItem getDataItem(Long id) {
		try {
			return dataItemDao.get(id);
		} catch (ObjectNotFoundException e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	@Transactional(readOnly = true)
	public DataDic getByName(String name) {
		try {
			return dataDicDao.findUniqueBy("name", name);
		} catch (ObjectNotFoundException e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	public void del(Long... ids) {
		for (Long id : ids) {
			if (notExist(id)) {
				continue;
			}
			try {
				dataDicDao.delete(id);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
				throw new BusinessException("发生异常!");
			}
		}
	}

	public void delDataItem(Long... ids) {
		for (Long id : ids) {
			if (itemNotExist(id)) {
				continue;
			}
			try {
				dataItemDao.delete(id);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
				throw new BusinessException("发生异常!");
			}
		}
	}

	private boolean itemNotExist(Long id) {
		return get(id) == null;
	}

	private boolean notExist(Long id) {
		return get(id) == null;
	}

	@Transactional(readOnly = true)
	public Page<DataDic> search(Page<DataDic> page, DataDicCriteria criteria) {
		try {
			dataDicDao.search(page, criteria);
			//			List<DataDic> result = page.getResult();
			//			List<DataDic> newResult = new ArrayList<DataDic>(result.size());
			//			for (DataDic dd : result) {
			//				DataDic coloneOne = new DataDic();
			//				BeanUtils.copyProperties(dd, coloneOne, new String[] { "items" });
			//				newResult.add(coloneOne);
			//			}
			//			page.setResult(newResult);
			return page;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}
}
